package com.handinglian.servicerecord.controller;

import com.apidoc.annotation.Api;
import com.apidoc.annotation.ApiVersion;
import com.github.pagehelper.PageInfo;
import com.handinglian.common.dto.ResultData;
import com.handinglian.common.factory.ResultDataFactory;
import com.handinglian.common.shiro.Principal;
import com.handinglian.servicerecord.dto.CallRecordAppDto;
import com.handinglian.servicerecord.dto.CallRecordDetailDto;
import com.handinglian.servicerecord.dto.CallRecordDto;
import com.handinglian.servicerecord.dto.IndexDto;
import com.handinglian.servicerecord.mapper.CallRecordMapper;
import com.handinglian.servicerecord.param.PushCallRecordParam;
import com.handinglian.servicerecord.service.CallRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Api("通话记录")
@RestController
@RequestMapping("/{version}/callrecord")
@ApiVersion(1.0)
public class CallRecordController {

    @Value("${file.download.location}")
    private String fileLocation;
    @Value("${file.download.url}")
    private String downloadUrl;
    @Autowired
    private CallRecordService callRecordService;
    @Autowired
    private CallRecordMapper callRecordMapper;


    /**
     * 获取通话记录分页列表
     */
    @RequiresPermissions("call_record:list")
    @GetMapping("/inquireCallRecordPageList")
    public ResultData<PageInfo<CallRecordDto>> inquireCallRecordPageList(Principal principal, String startDate, String endDate, String userNameOrJobNum, String mobilePhone, String recordStatus, String phoneStatus, String hasRecords, String sortField, String sortOrder, Integer pageIndex, Integer pageSize) {
        PageInfo<CallRecordDto> recordDtoPageInfo = callRecordService.inquireCallRecordPageList(principal.getUserId(), startDate, endDate,userNameOrJobNum, mobilePhone, recordStatus, phoneStatus, hasRecords, sortField, sortOrder, pageIndex, pageSize);
        return ResultDataFactory.generateSuccessResultData(recordDtoPageInfo);
    }

    /**
     * 获取通话记录(APP)
     */
    @RequiresPermissions("call_record:list")
    @GetMapping("/list")
    public ResultData<CallRecordAppDto> list(Principal principal, Integer answerState) {
        List<CallRecordAppDto> recordDtos = callRecordService.list(answerState, principal.getUserId());
        return ResultDataFactory.generateSuccessResultData(recordDtos);
    }

    /**
     * 获取通话详情
     */
    @RequiresPermissions("call_record:list")
    @GetMapping("/loadCallRecordDetail")
    public ResultData<CallRecordDetailDto> loadCallRecordDetail(Integer callrecordId) {
        CallRecordDetailDto callRecordDetailDto = callRecordService.loadCallRecordDetail(callrecordId);
        return ResultDataFactory.generateSuccessResultData(callRecordDetailDto);
    }

    /**
     * 下载录音
     */
    @RequiresPermissions("call_record:list")
    @GetMapping("/downloadRecording")
    public ResultData downloadRecording(String callid) throws IOException {
        return ResultDataFactory.generateSuccessResultData(callRecordService.downloadRecording(callid));
    }

    /**
     * 推送通话记录
     */
    @PostMapping("/pushCdrInfo")
    public String pushCdrInfo(@RequestBody PushCallRecordParam pushCallRecordParam) {
        int amount = callRecordService.createCallRecord(pushCallRecordParam.getCdr());
        return amount == 0?"failure":"success";
    }

    /**
     * 首页详情
     */
    @GetMapping("/getIndex")
    public ResultData<IndexDto> getIndex() throws ExecutionException, InterruptedException {
        IndexDto indexDto = callRecordService.getIndex();
        return ResultDataFactory.generateSuccessResultData(indexDto);
    }

    /**
     * 导出通话记录
     */
    @RequiresPermissions("call_record:list")
    @GetMapping("/exportCallRecord")
    public void exportCallRecord(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
        callRecordService.exportCallRecord(map, request, response);
    }
}
